package cn.funeralobjects.asms.service.impl;

import cn.funeralobjects.asms.entity.ESmsSendResult;
import cn.funeralobjects.asms.model.BasicInfo;
import cn.funeralobjects.asms.model.SmsSendResult;
import cn.funeralobjects.asms.repository.ESmsSendResultRepository;
import cn.funeralobjects.asms.service.SmsSendResultService;
import cn.funeralobjects.common.jpa.service.JpaEntityService;
import cn.funeralobjects.common.jpa.service.JpaModifyService;
import cn.funeralobjects.common.repository.EntityRepository;
import cn.funeralobjects.common.service.annotataion.CommonService;
import cn.funeralobjects.common.service.exception.DataConflictException;
import cn.funeralobjects.common.service.validate.DataCheck;
import cn.funeralobjects.util.Assert;
import cn.funeralobjects.util.NumberUtils;
import org.springframework.data.repository.CrudRepository;

import javax.annotation.Resource;

/**
 * @author FuneralObjects
 * Create date: 2020/5/31 11:11 PM
 */
@CommonService
public class SmsSendResultServiceImpl implements SmsSendResultService, JpaModifyService<SmsSendResult, ESmsSendResult, Integer>, JpaEntityService<ESmsSendResult, Integer> {

    @Resource
    private ESmsSendResultRepository eSmsSendResultRepository;

    @Override
    public void checkConflict(SmsSendResult smsSendResult) throws DataConflictException {
        DataCheck.conflictData(this.existsById(smsSendResult.getRecordId()), () -> createDataCode("recordId", String.valueOf(smsSendResult.getRecordId())));
    }

    @Override
    public ESmsSendResult toEntity(SmsSendResult smsSendResult) {
        return smsSendResult == null ? null : new ESmsSendResult()
                .setRecordId(smsSendResult.getRecordId())
                .setContent(smsSendResult.getContent())
                .setErrCode(smsSendResult.getErrCode())
                .setReceiveDate(smsSendResult.getReceiveDate())
                .setSendDate(smsSendResult.getSendDate())
                .setStatus(smsSendResult.getStatus());
    }

    @Override
    public EntityRepository<ESmsSendResult, Integer> entityRepository() {
        return eSmsSendResultRepository;
    }

    @Override
    public CrudRepository<ESmsSendResult, Integer> modifyRepository() {
        return eSmsSendResultRepository;
    }

    @Override
    public void checkConflict(SmsSendResult smsSendResult, Integer withoutId) throws DataConflictException {

    }

    @Override
    public void copyToEntity(SmsSendResult sourceModify, ESmsSendResult targetEntity) {
        targetEntity
                .setContent(sourceModify.getContent())
                .setErrCode(sourceModify.getErrCode())
                .setReceiveDate(sourceModify.getReceiveDate())
                .setSendDate(sourceModify.getSendDate())
                .setStatus(sourceModify.getStatus());
    }

    @Override
    public boolean isValid(Integer id) {
        return NumberUtils.notZero(id);
    }

    @Override
    public void validateModify(SmsSendResult smsSendResult) {
        Assert.argAssert(smsSendResult, "smsSendResult");
    }

    @Override
    public String getName() {
        return ENTITY_NAME;
    }

    @Override
    public String getModuleName() {
        return BasicInfo.MODULE_NAME;
    }
}
